<template>
  <a-modal
    :open="open"
    width="640px"
    title="装备分类"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    destroy-on-close
    @ok="onOk"
    @cancel="onCancel"
  >
    <div class="flex">
      <div class="w-full form-style-flex">
        <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '110px' } }">
          <a-form-item name="parentId" label="上级目录" class="w-full">
            <SelectAsset v-model:value="formState.parentId" />
          </a-form-item>
          <!-- v-bind="validateInfos.zjm" -->
          <a-form-item name="classificationName" label="资产分类名称" class="w-full" v-bind="validateInfos.classificationName">
            <a-input v-model:value="formState.classificationName" placeholder="请输入" />
          </a-form-item>
          <a-form-item name="expectedLimit" label="预计使用年限" class="w-full">
            <a-input v-model:value="formState.expectedLimit" placeholder="请输入" />
          </a-form-item>
          <a-form-item name="unit" label="单位" class="w-full">
            <a-input v-model:value="formState.unit" placeholder="请输入" />
          </a-form-item>
          <a-form-item name="memo" label="备注" class="w-full">
            <a-textarea v-model:value="formState.memo" :rows="2" placeholder="请输入" />
          </a-form-item>
        </a-form>
      </div>
    </div>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])

const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  },
  title: {
    type: String,
    default: ''
  }
})

const formState = ref({
  parentId: '',
  classificationName: '',
  unit: '',
  expectedLimit: '',
  memo: ''
})
const rulesState = ref({
  classificationName: [{ required: true, message: '请输入资产分类名称' }]
})

const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  await validate()

  const res = await post('/war/equipmentClassification/doSaveEquipmentClassification', formState.value)
  if (res.code === 0) {
    message.success(res.msg)

    emit('success')
    onCancel()
  } else {
    message.error(res.msg)
  }
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.rowData?.id) {
        formState.value = { ...props.rowData }
      } else {
        formState.value = { parentId: props.rowData?.parentId }
      }
    }
  }
)

const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
</script>
<style lang="less" scoped></style>
